﻿using MediatR;
using Newtonsoft.Json;
using Org.BouncyCastle.Math;
using W.Common;
using W.Product.API;
using W.Product.Domain.Dto;
using W.Product.Domain.Product;
using W.Product.Read.API.Appliaction.Command.ProductCommand;
using W.Product.Repository.Interface;
using static WMSpublic.ParseAPI;

namespace W.Product.Read.API.Appliaction.CommHandler.ProductCommandHandler
{
    public class ProductRangeFanCommandHandler : IRequestHandler<ProductRangeFanCommand, ApiResult<List<ProductDto>>>
    {
        private readonly IProductRepository _productRepository;
        private readonly IProductTypeRepository _productTypeRepository;
        public ProductRangeFanCommandHandler(IProductRepository productRepository, IProductTypeRepository productTypeRepository)
        {
            _productRepository = productRepository;
            _productTypeRepository = productTypeRepository;
        }
        /// <summary>
        /// 批量反填数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<ProductDto>>> Handle(ProductRangeFanCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<ProductDto>> result = new ApiResult<List<ProductDto>>();
            try
            {
                //ProductDto product = new ProductDto();
                //var res = (await _productRepository.GetAll()).Where(x => request.productId.Contains(x.ProductId)).ToList();
                List<Unit> untis = await GetUnitShow(1, 100);
                var res = from a in await _productRepository.GetAll()
                          join b in untis on a.UnitId equals b.UnitId
                          where a.IsDelete == false
                          select new ProductDto
                          {
                              ProductId = a.ProductId,
                              ProductNo = a.ProductNo,
                              ProductName = a.ProductName,
                              ManuNo = a.ManuNo,
                              InsideNo = a.InsideNo,
                              UnitId = a.UnitId,
                              ProductTypeId = a.ProductTypeId,
                              GoOnline = a.GoOnline,
                              Online = a.Online,
                              ProductSpec = a.ProductSpec,
                              Price = a.ProductPrice,
                              ProductPrice = a.ProductPrice,
                              WarehouseId = a.WarehouseId,
                              BinLocationId = a.BinLocationId,
                              VendorId = a.VendorId,
                              ClientId = a.ClientId,
                              ProductDesc = a.ProductDesc,
                              ProductWrap = a.ProductWrap,
                              ProductWeight = a.ProductWeight,
                              officeId = a.officeId,
                              userid = a.userid,
                              UnitName = b.UnitName,
                          };
                if (request.productId != null)
                {
                    res = res.Where(x => request.productId.Contains(x.ProductId)).ToList();
                }
                result.Data = res.ToList();
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 单位
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetUnitShow(int pageIndex, int pageSize)
        {
            var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0Iiwic2lkIjoiMTg1OTgwNTA4MDc2NTczMDgxNiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIiLCJqdGkiOiI5MWU1MzVhMy1kZjVlLTQxNGQtODdkMy0zYjE3MmIzNmRhZWEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LqO5a2Q5ra1IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.9e371kkhbGOGIALMM210Z0Mhka1BhADJFTKFd8w3AJY";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://10.223.8.18:8000/purchaseRead/api/Unit/Handle?pageIndex=" + pageIndex + "&pageSize=" + pageSize;
            var response = await client.GetAsync(url);
            var json = JsonConvert.DeserializeObject<ApiResultList<Unit>>(response);
            return json.data.list;
        }
   
    /// <summary>
    /// 单位管理表
    /// </summary>
    public class Unit
    {
        public int UnitId { get; set; }
        /// <summary>
        /// 单位编号
        /// </summary>
        public string? UnitNo { get; set; }
        /// <summary>
        /// 单位名称
        /// </summary>
        public string? UnitName { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string? UnitRemarks { get; set; }
        /// <summary>
        /// 公司ID
        /// </summary>
        public string? office_id { get; set; }
        /// <summary>
        /// 用户ID
        /// </summary>
        public int? user_id { get; set; }

    }
    }
}
